﻿using Autofac;
using Autofac.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using SBlogCore.Common.Attributes;
using SBlogCore.Common.AutofacExtend.Modules;

namespace SBlogCore.Program
{
    [SkipScan]
    public class SBlogDiService
    {
        private IContainer? _container;
        public T GetServices<T>() where T : notnull
        {
            _container ??= DiCollections();
            return _container.Resolve<T>();
        }

        private static IContainer DiCollections()
        {
            IServiceCollection services = new ServiceCollection();
            services.AddSBlogServices();
            var builder = new ContainerBuilder();
            AutofacModuleRegister.BuilderLoad(builder);
            builder.Populate(services);
            var applicationContainer = builder.Build();
            return applicationContainer;
        }
    }
}
